<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报告名称" prop="reportName">
        <el-input
          v-model="queryParams.reportName"
          placeholder="请输入报告名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="customerPhone">
        <el-input
          v-model="queryParams.customerPhone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号" prop="customerCardid">
        <el-input
          v-model="queryParams.customerCardid"
          placeholder="请输入证件号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报告状态" prop="reportStatus">
        <el-select v-model="queryParams.reportStatus" placeholder="请选择报告状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_report_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报告时间">
        <el-date-picker
          v-model="daterangeReportTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:report:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:report:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:report:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:report:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleUpload"
          v-hasPermi="['system:report:add']"
        >上传</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handlesend"
          v-hasPermi="['system:report:send']"
        >报告通知</el-button>
      </el-col>



      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="reportId" />
<!--      <el-table-column label="查询二维码" align="center" prop="reportCode"/>-->

      <el-table-column label="姓名" align="center" prop="customerName"/>

      <el-table-column label="手机号" align="center" prop="customerPhone" />

      <el-table-column label="证件号" align="center" prop="customerCardid"/>

      <el-table-column label="报告名称" align="center" prop="reportName"/>

      <el-table-column label="报告地址" align="center" prop="reportUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.reportUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="报告时间" align="center" prop="reportTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报告状态" align="center" prop="reportStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_report_status" :value="scope.row.reportStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="报告建议" align="center" prop="reportAdvice"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:report:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:report:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['monitor:operlog:query']"
          >查看更多报告</el-button>

        </template>



      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检查报告业务字段对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="证件号" prop="customerCardid" >
          <el-input v-model="form.customerCardid" placeholder="请输入证件号" />
        </el-form-item>
        <el-form-item label="报告名称" prop="reportName">
          <el-input v-model="form.reportName" placeholder="请输入报告名称" />
        </el-form-item>
        <el-form-item label="报告时间" prop="reportTime">
          <el-date-picker clearable
                          v-model="form.reportTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择报告时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报告状态">
          <el-radio-group v-model="form.reportStatus">
            <el-radio
              v-for="dict in dict.type.sys_report_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报告建议" prop="reportAdvice">
          <el-input v-model="form.reportAdvice" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="报告地址">
          <image-upload v-model="form.reportUrl"/>
        </el-form-item>
<!--        <el-form-item label="查询二维码" prop="reportCode">
          <el-input v-model="form.reportCode" type="textarea" placeholder="请输入内容" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog width="80%" custom-class="upload-report" :visible.sync="openUpload" :before-close="handleUploadClose" append-to-body>
      <image-upload :upload-img-url="imageUploadUrl" ref="fileUpload"></image-upload>
    </el-dialog>

  </div>
</template>

<script>
import {listReport, getReport, delReport, addReport, updateReport, sendReport} from "@/api/system/report";

import ImageUpload from "@/components/ImageUpload";

export default {
  name: "Report",
  components: { ImageUpload },
  dicts: ['sys_report_status'],
  data() {
    return {
      imageUploadUrl: process.env.VUE_APP_BASE_API + "/system/report/upload",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查报告业务字段表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示批量上传
      openUpload: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportName: null,
        customerName: null,
        customerPhone: null,
        customerCardid: null,
        reportStatus: null,
        reportAdvice: null,
        reportUrl: null,
        reportTime: null,
        reportCode: null
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        customerPhone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码11",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询检查报告业务字段列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeReportTime && '' != this.daterangeReportTime) {
        this.queryParams.params["beginReportTime"] = this.daterangeReportTime[0];
        this.queryParams.params["endReportTime"] = this.daterangeReportTime[1];
      }
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reportId: null,
        reportName: null,
        customerName: null,
        customerPhone: null,
        customerCardid: null,
        reportStatus: "0",
        reportAdvice: null,
        reportUrl: null,
        reportTime: null,
        reportCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reportId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检查报告业务字段";
    },
    /** 发送按钮操作 */
    handlesend() {
      this.$modal.confirm('是否发送今日上传报告成功通知？').then(function() {
        return sendReport();
      }).then(() => {

        this.$modal.msgSuccess("发送成功");
      }).catch(() => {});
    },

    /** 详细按钮操作 */
    handleView(row) {
      console.log(row.customerCardid);
      //获取当前身份证下所有报告
      this.open = true;

      this.form = row;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reportId = row.reportId || this.ids
      getReport(reportId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检查报告业务字段";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reportId != null) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reportIds = row.reportId || this.ids;
      this.$modal.confirm('是否确认删除检查报告业务字段编号为"' + reportIds + '"的数据项？').then(function() {
        return delReport(reportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    },

    /** 上传按钮操作 */
    handleUpload() {
      this.openUpload = true
    },
    handleUploadClose() {
      this.$refs.fileUpload.fileList = []
      this.$refs.fileUpload.uploadList = []
      this.openUpload = false
      this.reset()
      this.getList()
    }
  }
};
</script>

<style>
 .upload-report {
    height: 60vh !important;
  }
</style>
